import Cookies from 'js-cookie'
import store from '@/store'


export function isLogin() {
  const {token, uid} = getLogin();

  return token && uid;
}

export function getLogin() {
  let userInfo = localStorage.getItem(consts.CACHE_USER_INFO);
  if (userInfo) {
    userInfo = JSON.parse(userInfo)
    store.commit('USER_INFO',userInfo)
  }
  return {
    token: Cookies.get(consts.CACHE_TOKEN),
    uid: Cookies.get(consts.CACHE_UID),
    userInfo: userInfo
  }
}

export function setLogin(data) {
  Cookies.set(consts.CACHE_TOKEN, data.token);
  Cookies.set(consts.CACHE_UID, data.userId);
  localStorage.setItem(consts.CACHE_USER_INFO, JSON.stringify(data));
}

export function logout() {
  Cookies.remove(consts.CACHE_TOKEN);
  Cookies.remove(consts.CACHE_UID);
  localStorage.removeItem(consts.CACHE_USER_INFO);
  setTimeout(()=>{
    window.location.reload();
  },1500);
}
